package com.jhw.ymcc.config;

import com.jhw.ymcc.service.IPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;



@Configuration // 标识当前类是一个配置类，同时他也有@Component
@EnableWebSecurity // 开启web安全支持
@EnableGlobalMethodSecurity(prePostEnabled=true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private IPermissionService permissionService;

    //配置认证管理器，授权模式为“poassword”时会用到
    @Bean
    public AuthenticationManager authenticationManager() throws Exception {
        // OAuth2本身自己没办法进行认证，所以他需要依赖于security进行认证，那么我们就需要提供一个AuthenticationManager
        return super.authenticationManager();
    }

    /*
     * @Description: 提供用户信息放入到内存中，此处直接写死用户信息
     * @Author: Director
     * @Date: 2022/10/10 17:16
     * @return: org.springframework.security.core.userdetails.UserDetailsService
     **/
//    @Bean
//    public UserDetailsService userDetailsService(){
//        // 1.创建一个存放用户信息的基于内存的管理器对象
//        InMemoryUserDetailsManager inMemoryUserDetailsManager = new InMemoryUserDetailsManager();
//        // 2.往内存中创建一个用户信息
//        inMemoryUserDetailsManager.createUser(User.withUsername("zs").password("123").authorities("admin").build());
//        return inMemoryUserDetailsManager;
//    }

    public static void main(String[] args) {
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        System.out.println(bCryptPasswordEncoder.encode("123"));
        System.out.println(bCryptPasswordEncoder.encode("123"));
        System.out.println(bCryptPasswordEncoder.matches("123","$2a$10$8rDSUmlLVide27W/XwCbnOMS4.vt0EI1IP68YDhwRHzWnRk.xxucG"));
        System.out.println(bCryptPasswordEncoder.matches("123","$2a$10$MQCtt132xipzuWiLc5IhyenwbjZmHbsDI0TP6RvSkdsZC1EEb3c5y"));
    }


    /*
     * @Description: 密码编译器，获取到的密码是否需要加密之后比较，此处选择不加密
     * @Author: Director
     * @Date: 2022/10/10 17:17
     * @return: org.springframework.security.crypto.password.PasswordEncoder
     **/
    @Bean
    public PasswordEncoder passwordEncoder(){
        // 此处我们使用不加密的对象
        // return NoOpPasswordEncoder.getInstance();

        // 这样返回就是要加密比对
        return new BCryptPasswordEncoder();
    }

    /*
     * @Description: 授权规则配置，比如拦截哪些路径放行哪些路径
     * @Author: Director
     * @Date: 2022/10/10 17:17
     * @param http: 安全配置对象
     * @return: void
     **/
    @Override
    protected void configure(HttpSecurity http) throws Exception {

        // web授权，也就是告诉security哪些资源需要哪些权限才能访问
//        List<Permission> permissions = permissionService.selectList(null);
//        permissions.forEach(p->{
//            try {
//                http.authorizeRequests()
//                        .antMatchers(p.getResource()).hasAnyAuthority(p.getSn());
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        });

        http.authorizeRequests()                                //授权配置
                .antMatchers("/login").permitAll()  //登录路径放行
                .anyRequest().authenticated()                   //其他路径都要认证之后才能访问
                .and().formLogin()                              //允许表单登录
                .successForwardUrl("/login/success")             // 设置登陆成功页
                .and().logout().permitAll()                    //登出路径放行 /logout
                .and().csrf().disable();                        //关闭跨域伪造检查
    }
}